package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"wangblog/model"
	"wangblog/utils/errmsg"
)

//添加文章
func AddArticle(c *gin.Context){
	//todo 添加用户
	var data model.Article
	_=c.ShouldBindJSON(&data)
	model.CreateArt(&data)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"mesage":errmsg.GetErrMsg(code),
	})
}

//todo 查询分类下地所有文章
func GetCateArt(c *gin.Context){
	id,_:=strconv.Atoi(c.Param("id"))
	pageSize,_:=strconv.Atoi(c.Query("pagesize"))
	pageNum,_:=strconv.Atoi(c.Query("pagenum"))
	if pageSize==0{
		pageSize=-1
	}
	if pageNum==0{
		pageNum=-1
	}
	data,code,total:=model.GetCateArt(id,pageSize,pageNum)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"total":total,
		"message":errmsg.GetErrMsg(code),
	})
}

//todo 查询单个文章信息
func GetArtInfo(c *gin.Context){
	id,_:=strconv.Atoi(c.Param("id"))
	data,code:=model.GetArtInfo(id)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"message":errmsg.GetErrMsg(code),
	})
}

//todo 查询文章列表
func GetArt(c *gin.Context)  {
	pageSize,_:=strconv.Atoi(c.Query("pagesize"))
	pageNum,_:=strconv.Atoi(c.Query("pagenum"))
	if pageSize==0{
		pageSize=-1
	}
	if pageNum==0{
		pageNum=-1
	}
	data,code,total:=model.GetArt(pageSize,pageNum)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"total":total,
		"message":errmsg.GetErrMsg(code),
	})
}


//编辑文章
func EditArt(c *gin.Context)  {
	var data model.Article
	id,_:=strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	model.EditArt(id,&data)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"message":errmsg.GetErrMsg(code),
	})
}
//删除文章
func DeleteArt(c *gin.Context){
	id,_:=strconv.Atoi(c.Param("id"))
	code=model.DeleteArt(id)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"message":errmsg.GetErrMsg(code),
	})
}
